import logging
import os.path

import pytest

from resource.pom.settings import Settings
from utils.manager import Direction, Images


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-系统，点击软件更新')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.system_item)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('软件更新'))
        logging.info('预期结果1.进入软件更新')
        # 等动画加载完成
        device.wait(10)
        device.refresh_layout()
        device.assert_type_exist('Scroll')
        device.assert_text_exist('软件更新')
        device.assert_text_exist('检查更新')

        logging.info('步骤2.点击检查更新')
        device.click_element(device.get_element_by_text('检查更新'))
        # 没联网的情况下第一次点击会有弹窗，再点一次
        device.wait(6)
        device.click_element(device.get_element_by_text('检查更新'))
        update_picture_list = []
        # 动画过程截图
        for i in range(3):
            pic = device.snapshot_display('update{}.jpeg'.format(i))
            device.wait(1.5)
            update_picture_list.append(pic)
        for pic in update_picture_list:
            device.hdc_file_recv(pic)
        logging.info('预期结果2.界面有OpenHarmony检查更新动画，可更新至最新版本')
        local_files = [os.path.join(device.report_path, f) for f in os.listdir(device.report_path) if 'update' in f]
        # 动画图片裁切
        for f in local_files:
            Images.crop_picture(f)

        for i in range(len(local_files) - 1):
            similarity = Images.compare_image_similarity(local_files[i], local_files[i + 1])
            assert 0.1 < similarity < 1, '没有更新动画'
